package cn.xenosp.server.module.user.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.xenosp.server.common.Result;
import cn.xenosp.server.common.tools.IdUtil;
import cn.xenosp.server.common.tools.ObjectConvert;
import cn.xenosp.server.module.user.entity.SysUser;
import cn.xenosp.server.module.user.entity.SysUserVO;
import cn.xenosp.server.module.user.mapper.SysUserMapper;
import cn.xenosp.server.module.user.service.SysUserService;
import cn.xenosp.server.module.userRole.entity.SysUserRoleVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* @author TongHui
* @description 针对表【sys_user(用户信息表)】的数据库操作Service实现
* @createDate 2024-10-20 19:38:14
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService, UserDetailsService {

    @Override
    public IPage getPage(SysUserVO userVO) {
        SysUser sysUser = new SysUser();
        Page page = new Page(userVO.getPage(), userVO.getLimit());
        ObjectConvert.copyVoToEntity(userVO,sysUser);
        return baseMapper.page(page, sysUser);
    }

    @Override
    public SysUser getByUsername(String username) {
        return getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));
    }

    @Override
    public List<Map<String,Object>> userGetResources(String userId) {
        return baseMapper.selectMenuById(userId);
    }

    @Override
    public List<Map<String, Object>> userGetRole(String userId) {
        return baseMapper.selectRoleById(userId);
    }

    @Override
    public void addUser(SysUserVO userVO) {
        SysUser sysUser = new SysUser();
        ObjectConvert.copyVoToEntity(userVO,sysUser);
        sysUser.setId(IdUtil.getId());
        sysUser.setPassword(SaSecureUtil.md5(userVO.getUsername()+userVO.getPassword()));
        save(sysUser);
    }

    @Override
    public void updateUser(SysUserVO userVO) {
        SysUser sysUser = new SysUser();
        ObjectConvert.copyVoToEntity(userVO,sysUser);
        updateById(sysUser);
    }

    @Override
    public void updatePassword(SysUserVO userVO) {
        SysUser sysUser = getById(userVO.getId());
        SysUser user = new SysUser();
        user.setId(userVO.getId());
        user.setPassword(SaSecureUtil.md5(sysUser.getUsername()+userVO.getPassword()));
        updateById(user);
    }

    @Override
    public void delete(String userId) {
        removeById(userId);
    }

    @Override
    public void delete(String[] userIds) {
        if (ObjectUtil.isEmpty(userIds)){
            throw new NullPointerException();
        }
        removeBatchByIds(Arrays.asList(userIds));
    }

    @Override
    public Result<?> userVerify(SysUserVO userVO) {

        if (verifyUsername(userVO.getUsername()) >0){
            return Result.OK("账号重复，请重新输入。",1);
        }
        if (ObjectUtil.isNotEmpty(userVO.getMobile()) && verifyMobile(userVO.getMobile()) >0){
            return Result.OK("手机号重复，请重新输入。",1);
        }
        if (ObjectUtil.isNotEmpty(userVO.getEmail()) && verifyEmail(userVO.getEmail()) >0){
            return Result.OK("邮箱重复，请重新输入。",1);
        }
        return Result.OK("",0);
    }

    private int verifyUsername(String username) {
        SysUserVO userVO = new SysUserVO();
        userVO.setUsername(username);
        return baseMapper.userVerify(userVO);
    }
    private int verifyMobile(String mobile) {
        SysUserVO userVO = new SysUserVO();
        userVO.setMobile(mobile);
        return baseMapper.userVerify(userVO);
    }
    private int verifyEmail(String email) {
        SysUserVO userVO = new SysUserVO();
        userVO.setEmail(email);
        return baseMapper.userVerify(userVO);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = getByUsername(username);
        return new User(sysUser.getUsername(), sysUser.getPassword(), true, true, true, true, null);
    }
}




